<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
	//用户列显示list
    public function listindex()
    {
      //系统日志记录
      sys_log('访问会员列表');

      //个数
      $count = M('user')->count();
      $this->assign('num',$count);

        //所有用户显示出来
        $user = M('user');        
        // 查询满足要求的总记录数
        $count =  $user->count();
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        //问题显示  
        $users = $user->where('is_del=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('users',$users);        
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //show 显示
    public function show()
    {
      $id = I('get.id');

      //系统日志记录
      $str = '会员账号信息显示id='.$id;
      sys_log('会员账号信息显示',$str);

      $sql = 'select * from user where id='.$id;
      $user_row = M()->query($sql);
      $this->assign('user_row',current($user_row));

        $this->display();   
    }

    //addindex 显示
    public function listadd()
    {
        //系统日志记录
        sys_log('会员账号添加显示');

        $this->display();   
    }
    //add 
    public function add()
    {
        //print_r($_POST);exit;
        //用户
        $data['username'] = I('post.username');
        $user_Yz = M('user')->where(array('username'=>$data['username']))->find();
       if ($user_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"用户已存在"
                );
            echo json_encode($arr);
            exit;
       }
       //邮箱
       $data['email'] = I('post.email');
       $email_Yz = M('user')->where(array('email'=>$data['email']))->find();
       if ($email_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"邮箱已存在"
                );
            echo json_encode($arr);
            exit;
       }

       //入库
       $data['password'] = I('post.pass');
       $data['create_time'] = time();
       $data['create_ip'] =  get_client_ip();

       $res = M('user')->add($data);
       if ($res) {

        //系统日志记录
        $data = json_encode($data);
        $str = '会员账号添加id='.$res.'添加会员账号信息:'.$data;
        sys_log('会员账号添加',$str);

        $arr = array(
                'error' =>1,
                'info'=>"注册成功"
                );
        echo json_encode($arr);
       }else {
        $arr = array(
                'error' =>0,
                'info'=>"注册失败"
                );
        echo json_encode($arr);
       }

    }

    //编辑显示
    public function listedit()
    { 
        $id = I('get.id');

        //系统日志记录
        $str = '会员账号编辑id='.$id;
        sys_log('会员账号编辑显示',$str);

        $sql = 'select * from user where id='.$id;
        $userinfo = M()->query($sql);
        $this->assign('userinfo',current($userinfo));

        $this->display();
    }
    //编辑 异步接收
    public function edit()
    {
        //print_r($_POST);exit;
        //Array ( [email] => 2345@qq.com [username] => 234565 [sex] => 男 [city] => 5555 [sign] => 5555555555 )
        //用户
        $id = I('post.userid');
        $data['username'] = I('post.username');
        $user_Yz = M('user')->where(array('username'=>$data['username']))->find();
       if ($user_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"用户已存在"
                );
            echo json_encode($arr);
            exit;
       }
       //邮箱
       $data['email'] = I('post.email');
       $email_Yz = M('user')->where(array('email'=>$data['email']))->find();
       if ($email_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"邮箱已存在"
                );
            echo json_encode($arr);
            exit;
       }
       //入库
       $data['sex'] = I('post.sex');
       $data['city'] = I('post.city');
       $data['sign'] = I('post.sign');
       $data['edit_time'] = time();

       $res = M('user')->where(array('id'=>$id))->save($data);
       if ($res) {

        //系统日志记录
        $data = json_encode($data);
        $str = '会员账号编辑id='.$id.'添加会员编辑信息:'.$data;
        sys_log('会员账号编辑',$str);

        $arr = array(
                'error' =>1,
                'info'=>"修改成功"
                );
        echo json_encode($arr);
       }else {
        $arr = array(
                'error' =>0,
                'info'=>"修改失败"
                );
        echo json_encode($arr);
       } 
    }
    //修改密码页面 显示
    public function password()
    {
      $id = I('get.id');
      $sql = 'select id,username,password from user where id='.$id;
      $userinfo = M()->query($sql);
      $this->assign('userinfo',current($userinfo));

      //系统日志记录
      $str = '会员账号密码修改id='.$id;
      sys_log('会员账号密码修改显示',$str);

      $this->display();
    }
    //修改密码
    public function password_edit()
    {
     //print_r($_POST);exit;
     //Array ( [username] => 2345 [oldpass] => aaaaaaaaa [newpass] => ccccccccc [repass] => ccccccccc )
      $id = I('post.userid');
      $old = I('post.oldpass');
      $new = I('post.newpass');
      $renew = I('post.repass');

      $userinfo = M('user')->where(array('id'=>$id))->find();
      //当前密码
      if ($old!=$userinfo['password']) {
            $arr = array(
          "error" => 0,
          "info" => "旧密码错误"
              );
            echo json_encode($arr);
            exit;
      }
      //新密码
      $preg_pass = "/^[0-9A-z]{6,16}$/";
      $pass_Yz = preg_match($preg_pass, $new);
      if (!$pass_Yz) {
        $arr = array(
            "error" => 0,
            "info" => "密码必须是6~16位字符串"
          );
        echo json_encode($arr);
        exit;
      }
      //确认密码
      if ($new!=$renew) {
        $arr = array(
            "error" => 0,
            "info" => "确认密码输入有误"
          );
        echo json_encode($arr);
        exit;
     }
     //修改密码
     $sql = 'update user set password='.$new.' where id='.$id;
     $xiugai = M()->execute($sql);
     if ($xiugai) {

        //系统日志记录
        $str = '会员账号密码修改id='.$id.'会员密码修改前:'.$userinfo['password'].'会员密码修改后:'.$new;
        sys_log('会员账号密码修改',$str);

        $arr = array(
            "error" => 1,
            "info" => "密码修改成功"
          );
        echo json_encode($arr);
     }else {
        $arr = array(
            "error" => 0,
            "info" => "密码修改失败"
          );
        echo json_encode($arr);
     }
   }
    //用户停用 交互
    public function user_stop()
    {
        //print_r($_POST);exit;
      if (IS_POST) {
        $userid = I('post.userid');
        $sql = 'update user set is_denglu=0 where id='.$userid;
        $is_denglu = M()->execute($sql);
        if ($is_denglu) {

            //系统日志记录
            $str = '会员账号停用id='.$userid;
            sys_log('会员账号停用',$str);

            $arr = array(
                'error' =>0,
                'info'=>"用户停用成功",
                'userid' =>$userid
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' =>1,
                'info'=>"用户停用失败"
                );
            echo json_encode($arr);
        }

      }
        
    }
    //用户启用 交互
    public function user_start()
    {
        //print_r($_POST);
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update user set is_denglu=1 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {

            //系统日志记录
            $str = '会员账号启用id='.$userid;
            sys_log('会员账号启用',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户启用成功",
                  'userid' =>$userid
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户启用失败"
                  );
              echo json_encode($arr);
          }
      }

    }

    //del显示  0删除
    public function del()
    {
      //系统日志记录
      sys_log('会员账号删除显示');

      //个数
      $count = M('user')->count();
      $this->assign('num',$count);

      $user = M('user');          // 实例化question对象
      //$count      = $question->where('status=1')->count();// 查询满足要求的总记录数
      $count =  $user->count();
      $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
      //$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
      //问题显示  
      $is_del = $user->where('is_del=0')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
      $this->assign('is_del',$is_del);        
      $this->assign('page',$show);// 赋值分页输出

      $this->display();
    }

    //del 删除状态 1未删除 0删除
    public function is_del()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update user set is_del=0 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {

            //系统日志记录
            $str = '会员账号删除id='.$userid;
            sys_log('会员账号删除',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
        }       
    }

    //删除状态为0的用户 恢复  改为1
    public function no_del()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update user set is_del=1 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {

            //系统日志记录
            $str = '会员账号恢复id='.$userid;
            sys_log('会员账号恢复',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户恢复成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户恢复失败"
                  );
              echo json_encode($arr);
          }
        }  
    }

    //彻底删除
    public function delete()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'delete from user where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {

            //系统日志记录
            $str = '会员账号彻底删除id='.$userid;
            sys_log('会员账号彻底删除',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户彻底删除失败"
                  );
              echo json_encode($arr);
          }
      }  
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('user')->where($w)->setField('is_del',0);
          if ($res) {

            //系统日志记录
            $str = '会员账号批量删除id='.$ids;
            sys_log('会员账号批量删除',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //批量恢复
    public function recovers()
    {
      //print_r($_POST);
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('user')->where($w)->setField('is_del',1);
        if ($res) {
          
          //系统日志记录
          $str = '会员账号批量恢复id='.$ids;
          sys_log('会员账号批量恢复',$str);

          $arr = array(
              'error' => 0,
              'info' => '恢复成功'
            );
          echo json_encode($arr);
        }else {
          $arr = array(
              'error' => 1,
              'info' => '恢复失败'
            );
          echo json_encode($arr);
        }


    }


}